//
// Created by hsj on 2024-1-10.
// 编译：gcc oob_send_win.c -LH:\mingw\mingw64\x86_64-w64-mingw32\lib -lws2_32 -o oob_send_win

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#define BUF_SIZE 30
void ErrorHandling(char *message);

int main(int argc, char *argv[]){
    WSADATA wsaData;
    SOCKET hSocket;
    SOCKADDR_IN sendAdr;
    char ip[]="127.0.0.1";
    char port[]="7777";

    if(WSAStartup (MAKEWORD(2, 2), &wsaData) != 0)
        ErrorHandling( "WSAStartup() error!");
    hSocket=socket(PF_INET, SOCK_STREAM, 0);
    memset(&sendAdr, 0, sizeof(sendAdr));
    sendAdr.sin_family=AF_INET;
    sendAdr.sin_addr.s_addr=inet_addr(/*argv[1]*/ip);
    sendAdr.sin_port=htons(atoi(/*argv[2]*/port));

    if(connect (hSocket, (SOCKADDR* )&sendAdr, sizeof(sendAdr))==SOCKET_ERROR)
        ErrorHandling(" connect() error!");

    send(hSocket, "123", 3, 0);
    send(hSocket, "4", 1, MSG_OOB);
    Sleep(2000);//sleep 2 sec
    send(hSocket, "567", 3, 0);
    send(hSocket, "890", 3, MSG_OOB);
    closesocket(hSocket);
    WSACleanup();

    return EXIT_SUCCESS;
}

void ErrorHandling( char *message){
    fputs (message, stderr);
    fputc('\n', stderr);
    exit(1);
}